<?php

namespace Izqut\Membership;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;

class MembershipServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        $this->mergeConfigFrom(
            __DIR__.'/../config/membership.php',
            'membership'
        );
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        // Publish config
        $this->publishes([
            __DIR__.'/../config/membership.php' => config_path('membership.php'),
        ], 'membership-config');

        // Publish migrations
        $this->publishes([
            __DIR__.'/../database/migrations/' => database_path('migrations'),
        ], 'membership-migrations');

        // Load migrations
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

        // Register Blade directives
        $this->registerBladeDirectives();
    }

    /**
     * Register custom Blade directives
     */
    protected function registerBladeDirectives(): void
    {
        // @vip directive
        Blade::if('vip', function () {
            return auth()->check() && auth()->user()->isVip();
        });

        // @membership directive
        Blade::if('membership', function () {
            return auth()->check() && auth()->user()->hasMembership();
        });

        // @membershipPlan('slug') directive
        Blade::if('membershipPlan', function (string $slug) {
            return auth()->check() && auth()->user()->hasMembershipPlan($slug);
        });
    }
}
